package com.sleep.getexcel.service.impl;

import com.mysql.cj.util.StringUtils;
import com.sleep.getexcel.dao.KpiIndexMapper;
import com.sleep.getexcel.pojo.KpiIndex;
import com.sleep.getexcel.service.KpiIndexService;
import com.sleep.getexcel.utils.ReadExcel;
import com.sleep.getexcel.vo.FilePath;
import com.sleep.getexcel.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Sleep-6000
 * @date 2022/8/3
 */
@Service
public class KpiIndexServiceImpl implements KpiIndexService {
    @Autowired
    private KpiIndexMapper kpiIndexMapper;

    @Override
    public Result insertSJYZ(FilePath filePath) {
        String readPath = filePath.getReadPath();
        if (StringUtils.isNullOrEmpty(readPath)) {
            return Result.fail(10003, "参数错误");
        }
        ReadExcel readExcel = new ReadExcel();
        if (!readExcel.preReadCheck(readPath)) {
            return Result.fail(10001, "文件不存在");
        }
        List<List<String>> read = readExcel.read(readPath);
        if (read.size() < 2) {
            return Result.fail(10002, "文件为空");
        }
        int count = kpiIndexMapper.getCount();
        int success = 0;
        String idxDept = read.get(1).get(4);
        for (int i = 1; i < read.size(); i++) {
            KpiIndex index = new KpiIndex();
            index.setIdxName(read.get(i).get(0));
            index.setIdxCode(read.get(i).get(1));
            index.setDataSrc(read.get(i).get(2));
            if ("sj".equals(idxDept)) {
                index.setIdxDept("sj");
                index.setColId(1534467731401736201L);
            } else if ("ir".equals(idxDept)) {
                index.setIdxDept("ir");
                index.setColId(1534467731401736202L);
            } else if ("steel".equals(idxDept)) {
                index.setIdxDept("steel");
                index.setColId(1534467731401736203L);
            } else if ("zg".equals(idxDept)) {
                index.setIdxDept("zg");
                index.setColId(1534467731401736204L);
            }
            index.setIdxFormula(read.get(i).get(3));
            index.setRootNode((byte) 0);
            index.setIdxCalPeriod("DAY");
            index.setIdxUnit(read.get(i).get(5));
            index.setIdxDirect(read.get(i).get(6));
            index.setNo(++count);
            success += kpiIndexMapper.insert(index);
        }
        return Result.success(success);
    }

    @Override
    public Result insertSJKZ(FilePath filePath) {
        String readPath = filePath.getReadPath();
        if (StringUtils.isNullOrEmpty(readPath)) {
            return Result.fail(10003, "参数错误");
        }
        ReadExcel readExcel = new ReadExcel();
        if (!readExcel.preReadCheck(readPath)) {
            return Result.fail(10001, "文件不存在");
        }
        List<List<String>> read = readExcel.read(readPath);
        if (read.size() < 2) {
            return Result.fail(10002, "文件为空");
        }
        int count = kpiIndexMapper.getCount();
        int success = 0;
        String idxDept = read.get(1).get(5);
        for (int i = 1; i < read.size(); i++) {
            KpiIndex index = new KpiIndex();
            index.setColId(Long.valueOf(read.get(i).get(0)));
            index.setIdxName(read.get(i).get(1));
            index.setIdxCode(read.get(i).get(2));
            index.setDataSrc(read.get(i).get(3));
            index.setTags(read.get(i).get(4));
            if ("sj".equals(idxDept)) {
                index.setIdxDept("sj");
            } else if ("ir".equals(idxDept)) {
                index.setIdxDept("ir");
            } else if ("steel".equals(idxDept)) {
                index.setIdxDept("steel");
            } else if ("zg".equals(idxDept)) {
                index.setIdxDept("zg");
            }
            index.setRootNode((byte) 0);
            index.setIdxCalPeriod("DAY");
            index.setIdxUnit(read.get(i).get(6));
            index.setIdxDirect(read.get(i).get(7));
            index.setNo(++count);
            success += kpiIndexMapper.insert(index);
        }
        return Result.success(success);
    }
}
